package com.tlgen.coopera.comon;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 响应信息
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class R implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = 200;

    /**
     * 失败
     */
    public static final int FAIL = 500;

    private int code;

    private String msg;

    private Object data;

    public static R ok() {
        return restResult(null, SUCCESS, null);
    }

    public static R ok(Object data) {
        return restResult(data, SUCCESS, "操作成功!");
    }

    public static  R ok(Object data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static  R fail() {
        return restResult(null, FAIL, null);
    }

    public static  R fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static  R fail(Object data) {
        return restResult(data, FAIL, null);
    }

    public static  R fail(Object data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static  R fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    private static  R restResult(Object data, int code, String msg) {
        return (R) R.builder().code(code).data(data).msg(msg).build();
    }

}
